package com.fyzk.activity.gl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;

@SuppressLint("NewApi")
public class RandomSetView extends LinearLayout {
	//最小个数
	private int minRandomNum = 0;
	//最大个数
	private int maxRandomNum = 0;
	//标题
	private String title;

	private TextView lineTitle;
	private Button countBtn;
	private Button jxaction;
	
	private Context context;
	
	private int choseNum = minRandomNum;
	
	public RandomSetView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context,attrs);
	}

	public RandomSetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context,attrs);
	}

	public RandomSetView(Context context) {
		super(context);
		initView(context,null);
	}
	
	private void initView(Context context, AttributeSet attrs){
		this.context = context;
		View.inflate(context, R.layout.layout_random_num, this);
		if (isInEditMode()) {
			return;
		}
		if (null != attrs) {
			TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.RandomSetView);
			title = type.getString(R.styleable.RandomSetView_lineTitle);
			minRandomNum = type.getInt(R.styleable.RandomSetView_minJxNm, 0);
			maxRandomNum = type.getInt(R.styleable.RandomSetView_maxJxNm, 0);
		}
		choseNum = minRandomNum;
		lineTitle = (TextView)findViewById(R.id.random_num_title);
		countBtn = (Button)findViewById(R.id.random_num_count);
		jxaction =(Button)findViewById(R.id.random_num_action);
		lineTitle.setText(title);
		countBtn.setText(String.valueOf(minRandomNum));
		jxaction.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener != null){
					listener.onRandomBtnClick(RandomSetView.this,choseNum);
				}
			}
		});
		countBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				createDialog();
			}
		});
		
	}
	
	public void setDefaultChoseNum(int defalutchoseNum){
		this.choseNum = defalutchoseNum;
		countBtn.setText(String.valueOf(defalutchoseNum));
	}
	
	
	public void setRandomBtnVisible(int visible){
		countBtn.setVisibility(visible);
		jxaction.setVisibility(visible);
	}
	
	public void setMinRandomNum(int minNum,int maxNum){
		minRandomNum = minNum;
		maxRandomNum = maxNum;
		choseNum = minRandomNum;
		countBtn.setText(String.valueOf(minRandomNum));
	}

	public void setLineTitle(int resId){
		lineTitle.setText(resId);
	}
	
	public void setLineTitle(String resId){
		lineTitle.setText(resId);
	}
	
	
	
	private PopupWindow popupwindow;
	
	private void createDialog() {
		LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View popupView = (LinearLayout) inflate.inflate(R.layout.buy_jx_btn_window, null);
		LinearLayout layout1 = (LinearLayout) popupView.findViewById(R.id.buy_jx_btn_layout1);
		LinearLayout layout2 = (LinearLayout) popupView.findViewById(R.id.buy_jx_btn_layout2);
//		if (!isRed) {
//			layout1.setBackgroundResource(R.drawable.buy_jx_btn_window_top_bg_b);
//			layout2.setBackgroundResource(R.drawable.buy_jx_btn_window_bg_b);
//		}
		layout1.getBackground().setAlpha(200);
		layout2.getBackground().setAlpha(200);
		popupwindow = new PopupWindow(popupView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		popupwindow.setTouchable(true); // 设置PopupWindow可触摸
		popupwindow.setOutsideTouchable(true);
		popupView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (popupwindow != null && popupwindow.isShowing()) {
					popupwindow.dismiss();
					popupwindow = null;
				}

				return false;
			}
		});
		initBtnWidth();
		addBtn(layout2, minRandomNum, maxRandomNum);
		popupwindow.showAsDropDown(countBtn);
	}
	
	private final int LINE_NUM = 8;
	
	private void addBtn(LinearLayout layoutView, int startNum, int sizeNum) {
		int allNum = sizeNum - startNum + 1;
		int line = allNum / LINE_NUM;
		int lastLineNum = allNum % LINE_NUM;
		int lineStart = startNum - 1;
		for (int i = 0; i < line; i++) {
			if (i == line - 1 && lastLineNum == 0) {
				lineStart = initLine(layoutView, lineStart, LINE_NUM, false);
			} else {
				lineStart = initLine(layoutView, lineStart, LINE_NUM, true);
			}
		}
		if ((lastLineNum > 0)) {
			lineStart = initLine(layoutView, lineStart, lastLineNum, false);
		}
	}
	
	private int width, padding;
	
	
	
	private void initBtnWidth() {
		int widthAll = AppUtil.getWidth();
		width = AppUtil.dip2px(context, 35f);
		padding = (widthAll - width * LINE_NUM) / (LINE_NUM + 1);
	}
	
	public interface RandomBtnClickListener{
		public void onRandomBtnClick(RandomSetView v,int randomCount);
	}
	
	RandomBtnClickListener listener;
	
	public void setOnRandomBtnClickListener(RandomBtnClickListener listener){
		this.listener = listener;
	}
	
	
	private int initLine(LinearLayout layoutView, int startNum, int lineNum, boolean isLast) {
		LinearLayout layout = new LinearLayout(context);
		for (int j = 0; j < lineNum; j++) {
			final Button btn = new Button(context);
			btn.setBackgroundResource(R.drawable.buy_jx_btn_bg_selector);
			startNum++;
			btn.setText(startNum + "");
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(width, width);
			param.setMargins(padding, padding, 0, 0);
			btn.setLayoutParams(param);
			btn.setTextSize(16);
			btn.setTextColor(getResources().getColor(R.color.top_bg));
			btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					choseNum = Integer.parseInt(btn.getText().toString());
					if(listener != null){
						listener.onRandomBtnClick(RandomSetView.this,choseNum);
					}
					
					countBtn.setText(String.valueOf(choseNum));
					popupwindow.dismiss();
				}
			});
			layout.addView(btn);
		}
		if (isLast) {
			layoutView.addView(layout);
		} else {
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

			param.setMargins(0, 0, 0, padding);
			layoutView.addView(layout, param);
		}
		return startNum;
	}

}
